import { ApiRes, CategoryItem, SubCategory, TopCategoryItem } from '@/types/data'
import  request  from '@/utils/request'
import {defineStore} from 'pinia'
import { topCategory } from '../constants'
const defaultCategory = topCategory.map((item) => {
  return {
    name: item,
  }
})

export default defineStore('category',{
  state() {
      return {
        categoryList :defaultCategory as CategoryItem[],
        topCategorys :{} as TopCategoryItem,
        subCategorys : {} as SubCategory
    }
  },
  actions:{
  async  getCategoryList(){
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      res.data.result.forEach(item => {
      item.open=false
    });
    this.categoryList = res.data.result
    },
    show(id:string){
      const isAll =   this.categoryList.find(item=>item.id === id )
      isAll!.open = true  

    },
    hide(id:string){
      const isAll =   this.categoryList.find(item=>item.id === id )
      isAll!.open = false
    },
    async getTopCategory(id:string){
      const res = await request.get<ApiRes<TopCategoryItem>>('/category',{params:{id}})
      this.topCategorys = res.data.result
    },
    async getSubCategory(id:string){
      const res = await request.get<ApiRes<SubCategory>>('/category/sub/filter',{params:{id}})
      this.subCategorys = res.data.result 
    },
    resetTopcategory(){
      this.topCategorys={} as TopCategoryItem
    },
    resetSubcategory(){
      this.subCategorys={} as SubCategory
    }
  }
})